﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class LoginAdmin : System.Web.UI.Page
{
    TblCustomersDAO customerDAO = new TblCustomersDAO(Global.CONNECTION_STRING);
    // New cookie
    HttpCookie cookie_username = null;
    HttpCookie cookie_pass = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Cookies["pass"] != null && Request.Cookies["username"] != null)
            {
                txtUsername.Text = Request.Cookies["username"].Value;
                txtPassword.Text = Request.Cookies["pass"].Value;
            }
        }
    }
    protected void cmdLogin_Click(object sender, EventArgs e)
    {
        bool bFlag = false;
        //TblCustomers customer = customerDAO.GetCustomer(txtUsername.Text.Trim(), Encryption.EncryptToString(txtPassword.Text));
        try
        {
            // khai báo biến cookie
            cookie_username = new HttpCookie("username", txtUsername.Text);
            cookie_pass = new HttpCookie("pass", txtPassword.Text);

            // Gan ngay song cho cookie
            cookie_username.Expires = DateTime.Now.AddDays(60);
            cookie_pass.Expires = DateTime.Now.AddDays(60);

            //Them cookie

            if (cbRemember.Checked)
            {
                cookie_username.Value = txtUsername.Text;
                cookie_pass.Value = txtPassword.Text;
                Response.Cookies.Add(cookie_username);
                Response.Cookies.Add(cookie_pass);
            }
            else
            {
                cookie_username.Value = null;
                cookie_pass.Value = null;
            }

            if (txtPassword.Text != null || txtUsername.Text != null)
            {
                TblCustomers customer = customerDAO.GetCustomer(txtUsername.Text.Trim(), Encryption.EncryptToString(txtPassword.Text));

                if ((customer.Emailaddress == txtUsername.Text) && (customer.Emailpassword == Encryption.EncryptToString(txtPassword.Text)) && (customer.IsAdmin == true))
                {
                    Session[Global.SESSION_LOGGED_USER] = customer;
                    bFlag = true;

                }
                else
                {
                    lblMessage.Text = "UserName or Password is not available.";
                    lblMessage.Visible = true;
                }
            }
            
        }

        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
        if (bFlag)
        {
            Response.Redirect("~/Admin/JobListNonWarranty.aspx");
        }
    }
}
